<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Messages</title>
    <!-- 引入Bootstrap CSS -->
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <!-- 引入jQuery -->
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }
        .container {
            max-width: 960px;
            margin: 0 auto;
            padding: 20px;
        }
        .table {
            width: 100%;
            margin: 20px 0;
            border-collapse: collapse;
        }
        .table th,
        .table td {
            padding: 10px;
            border: 1px solid #ddd;
        }
        .table th {
            background-color: #333;
            color: #fff;
        }
        .table td {
            background-color: #fff;
            color: #333;
        }
        .pagination {
            list-style-type: none;
            padding: 0;
            display: flex;
            justify-content: center;
        }
        .page-link {
            color: blue;
            font-size: 16px;
            text-decoration: none;
            padding: 5px 10px;
            border: 1px solid transparent;
        }
        .page-link:hover {
            border-color: blue;
        }
        .page-item {
            margin: 0 5px;
        }

        .page-link {
            display: inline-block;
            padding: 0.75rem 1.25rem;
            margin-left: -1px;
            line-height: 1.5;
            color: #007bff;
            background-color: #fff;
            border: 1px solid #dee2e6;
            border-radius: 0.25rem;
            text-decoration: none;
        }

        .page-link:hover {
            color: #0056b3;
            background-color: #e9ecef;
            border-color: #dee2e6;
        }
        .be-pager-total {
            display: flex;
            font-size: 16px;
            color: #333;
            margin-right: 10px;
            align-items: center;
        }

        .be-pager-options-elevator {
            display: flex;
            font-size: 16px;
            color: #333;
            margin-right: 10px;
            align-items: center;
        }

        .space_input {
            display: flex;
            width: 50px;
            height: 20px;
            margin-left: 5px;
            margin-right: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            text-align: center;
            align-items: center;
        }
    </style>
</head>
<body>
<div class="container mt-5">
    <h2>消息数据</h2>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>序号</th>
                <th>创建时间</th>
                <th>用户名称</th>
                <th>消息内容</th>
            </tr>
        </thead>
        <tbody id="tableBody"></tbody>
    </table>
    <nav>
        <ul class="pagination">
        <li class="page-item" id="prevPage">
        <a class="page-link" href="javascript:void(0)">上一页</a>
        </li>
        <li class="page-item">
        <a id="currentNum" class="page-link" href="javascript:void(0)">1</a>
        </li>
        <li class="page-item" id="nextPage">
        <a class="page-link" href="javascript:void(0)">下一页</a>
        </li>

        <span id="totalRecord" class="be-pager-total"></span>
        <span class="be-pager-options-elevator">
        跳至
        <input id="jump_input" type="text" class="space_input"> 页
        </span>
</ul>
    </nav>
</div>
<script>
    $(document).ready(function() {
        var currentPage = 1;
        var pageSize = 10;
        var totalPages = 5;
        var totalRecord = 0;
        function getRecordNum () {
            $.ajax({
                url: '/api/getRecordNum',
                type: 'GET',
                dataType: 'json',
                success: function(data) {
                    totalRecord = data;
                    totalPages = Math.ceil(totalRecord/10);
                    // $("#totalRecord").text("共 "+totalPages+"页   合计"+totalRecord+" 条记录");
                    $("#totalRecord").text("共 "+totalPages+" 页");

                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log('请求失败:', textStatus, errorThrown);
                }
            });
        }
        function loadMessages(page) {
            $.get('/api/messages2', {page: page, size: pageSize}, function(data) {
                $('#tableBody').empty();
                console.log(data)
                data.forEach(function(message) {
                    var row = $('<tr></tr>');
                    row.append($('<td></td>').text(message.id));
                    row.append($('<td></td>').text(new Date(Number(message.create_time) * 1000).toISOString().slice(0, 19).replace('T', ' ')));
                    row.append($('<td></td>').text(message.user_name));
                    row.append($('<td></td>').text(message.content));
                    $('#tableBody').append(row);
                });
            });
        }
        $('#nextPage').on('click', function(e) {
            e.preventDefault();
            if (currentPage < totalPages) {
                currentPage++;
                loadMessages(currentPage);
                renderPageNum(currentPage)
            }
        });
        $('#prevPage').on('click', function(e) {
            e.preventDefault();
            if (currentPage > 1) {
                currentPage--;
                loadMessages(currentPage);
                renderPageNum(currentPage)
            }
        });
       $('#jump_input').on('keypress', function(e) {
        var keycode = (e.keyCode ? e.keyCode : e.which);
        // console.log(keycode);


        if (keycode == '13') {
            var jumpPage = $('#jump_input').val();
            // Enter key pressed
            // Perform your action here
            if (jumpPage > totalPages) {
                alert("输入页数超过总页数");
                return;
            }
            if (jumpPage < 1) {
                alert("输入页数小于1");
                return;
            }
            currentPage = jumpPage;
            loadMessages(currentPage);
            renderPageNum(currentPage);
            console.log(jumpPage);
        }
    });
        function renderPageNum(currentPage) {
            $("#currentNum").text(currentPage);
        }
        loadMessages(currentPage);
        getRecordNum()
    });
</script>
</body>
</html>